﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using UserManage;
using Eb.MessageBox;
public partial class ucUtilityGroup : System.Web.UI.UserControl
{
    UserManager _utilityGroupManager = new UserManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        btnBack.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnBack.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");

        btnSave.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSave.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnAddUtilityGroup.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAddUtilityGroup.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");


        if (!IsPostBack)
        {
            InitializeDDL();
        }
    }
    protected void ddlUtilityGroup_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlUtilityGroup.SelectedIndex!=0)
            GetUtilities();
        //LoadData();
    }
    protected void btnAddUtility_Click(object sender, EventArgs e)
    {
        //if (lstUtilities.SelectedIndex == -1)
        //{
        //    Response.Write("<script>alert('Select Utility')</script>");
        //    return;
        //}
        ////foreach (ListItem l in lstSelectedUtilities.Items)
        ////{
        ////    if (lstUtilities.SelectedItem == l)
        ////    {
        ////        Response.Write("<script>alert('Already Added')</script>");
        ////        return;
        ////    }
        ////}
        //lstSelectedUtilities.Items.Add(new ListItem(lstUtilities.SelectedItem.ToString(),lstUtilities.SelectedValue));
    }
   
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (ddlUtilityGroup.SelectedIndex == 0)
        {
            MessageBox.Show("Select a Group.");
          //  Response.Write("<script>alert('Select a Group')</script>");
            return;
        }
        else
        {
            AddUtilities();
            RemoveUtilities();
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        RemoveUtilities();
        //LoadData();
        GetUtilities();
    }

    void InitializeDDL()
    {
        DataSet dsUtilityGroup = _utilityGroupManager.GetData("UtilityGroup", string.Empty);

        ddlUtilityGroup.DataSource = dsUtilityGroup;
        ddlUtilityGroup.DataTextField = "UG_GroupName";
        ddlUtilityGroup.DataValueField = "UG_GroupID";
        ddlUtilityGroup.DataBind();
        ddlUtilityGroup.Items.Insert(0, "Select");
    }

    //void LoadData()
    //{
    //    if (ddlUtilityGroup.SelectedIndex == 0)
    //    {
    //        Response.Write("<script>alert('Select a Utility Group')</script>");
    //        return;
    //    }
    //    string tableName = "UtilityList";
    //    string condition = "UT_UtilityGroupID = '" + ddlUtilityGroup.SelectedValue + "'";
    //    lstUtilities.Items.Clear();
    //    lstSelectedUtilities.Items.Clear();
    //    DataSet dsLoad = _utilityGroupManager.GetData(tableName, string.Empty);
    //    for (int i = 0; i < dsLoad.Tables[0].Rows.Count; i++)
    //    {
    //        lstUtilities.Items.Add(new ListItem(dsLoad.Tables[0].Rows[i][1].ToString(), dsLoad.Tables[0].Rows[i][0].ToString()));
    //    }
    //    dsLoad.Clear();

    //    dsLoad = _utilityGroupManager.GetData(tableName, condition);
    //    for (int i = 0; i < dsLoad.Tables[0].Rows.Count; i++)
    //    {
    //        lstSelectedUtilities.Items.Add(new ListItem(dsLoad.Tables[0].Rows[i][1].ToString(), dsLoad.Tables[0].Rows[i][0].ToString()));
    //    }
     
               
    //}

    void GetUtilities()
    {
        string condition = "UT_UtilityGroupID = '" + ddlUtilityGroup.SelectedValue + "'";
        DataSet ds = _utilityGroupManager.GetData("UtilityList", condition);
        lstSelectedUtilities.DataSource = ds;
        lstSelectedUtilities.DataTextField = "UT_UtilityName";
        lstSelectedUtilities.DataValueField = "UT_UtilityID";
        lstSelectedUtilities.DataBind();

        //        SELECT     T1.*,T2.* 
        //FROM        UserRolesList as T1,UserRolesMap as T2 where T1.UR_RoleID not in (select UM_RoleID from UserRolesMap where UM_UserID = 25)

        condition = "UT_UtilityGroupID <> '" + ddlUtilityGroup.SelectedValue + "' OR UT_UtilityGroupID IS NULL";
        ds.Clear();
        ds = _utilityGroupManager.GetData("UtilityList", condition);
        lstUtilities.DataSource = ds;
        lstUtilities.DataTextField = "UT_UtilityName";
        lstUtilities.DataValueField = "UT_UtilityID";
        lstUtilities.DataBind();

    }

    void AddUtilities() //add utilities to the group
    {

        try
        {
            string groupID, condition;
            string[] utilities;
            if (lstSelectedUtilities.Items.Count == 0)
            {
                MessageBox.Show("There Are No Utilities In The Group.");
              //  Response.Write("<script>alert('There Are No Utilities In The Group')</script>");
                return;
            }

            condition = "UT_UtilityID = " + lstSelectedUtilities.Items[0].Value + "";
            for (int i = 1; i < lstSelectedUtilities.Items.Count; i++)
            {
                condition = condition + " or UT_UtilityID = " + lstSelectedUtilities.Items[i].Value + "";
            }
            groupID = ddlUtilityGroup.SelectedValue;
            if (_utilityGroupManager.AddRemoveUtility(condition, groupID))
                MessageBox.Show("Successfully added");
               // Response.Write("<script>alert('Added Successfully!')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Move Utility Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
    }

    void RemoveUtilities() //remove utilities
    {
        try
        {
            //string condition = "UT_UtilityID = " + lstSelectedUtilities.SelectedValue + "";
            //if (lstSelectedUtilities.SelectedIndex == -1)
            //{
            //    Response.Write("<script>alert('Select Utility')</script>");
            //    return;
            //}
            string condition, groupID;
            groupID = ddlUtilityGroup.SelectedValue;
            if (lstUtilities.Items.Count != 0)
            {
                condition = "UT_UtilityGroupID ='" + groupID + "' and (UT_UtilityID = " + lstUtilities.Items[0].Value + "";
                for (int i = 1; i < lstUtilities.Items.Count; i++)
                {
                    condition = condition + " or UT_UtilityID = " + lstUtilities.Items[i].Value + "";
                }
                condition = condition + ")";
                _utilityGroupManager.AddRemoveUtility(condition, "");
            }
            //condition = "UT_UtilityGroupID ='" + groupID + "' ";
            //if (lstUtilities.Items.Count != 0)
            //{
            //    condition = "and (UT_UtilityID = " + lstUtilities.Items[0].Value + "";
            //    for (int i = 1; i < lstUtilities.Items.Count; i++)
            //    {
            //        condition = condition + " or UT_UtilityID = " + lstUtilities.Items[i].Value + "";
            //    }
            //    condition = condition + ")";
            //}
                      
           

            //if (_utilityGroupManager.AddRemoveUtility(condition, ""))
            //    Response.Write("<script>alert('Deleted Successfully!')</script>");

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Delete Utility Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    protected void lnbUsers_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserSearch.aspx");
    }
    protected void lnbRoles_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserRoleSearch.aspx");
    }

    protected void lnbUtilities_Click(object sender, EventArgs e)
    {
        Response.Redirect("Utility Search.aspx");
    }
    protected void lnbPermissions_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddPermission.aspx");
    }
   
    protected void lnbHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserHome.aspx");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucSearchAddUtility.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    protected void lnbUsers_Click1(object sender, EventArgs e)
    {

    }
    protected void btnMove4_Click(object sender, EventArgs e)
    {
        if (lstSelectedUtilities.SelectedIndex == -1)
        {
            Response.Write("<script>alert('Select Utility')</script>");
            return;
        }
        lstUtilities.Items.Add(new ListItem(lstSelectedUtilities.SelectedItem.ToString(), lstSelectedUtilities.SelectedValue));
        lstSelectedUtilities.Items.RemoveAt(lstSelectedUtilities.SelectedIndex);
    }
    protected void btnMove3_Click(object sender, EventArgs e)
    {    
        int flag;
        for (int i = 0; i < lstSelectedUtilities.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstUtilities.Items.Count; j++)
                if (lstSelectedUtilities.Items[i].Value == lstUtilities.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstUtilities.Items.Add(new ListItem(lstSelectedUtilities.Items[i].Text, lstSelectedUtilities.Items[i].Value));
            }
        }
        lstSelectedUtilities.Items.Clear();
    }
    protected void btnMove2_Click(object sender, EventArgs e)
    {
        int flag;
        for (int i = 0; i < lstUtilities.Items.Count; i++)
        {
            flag = 0;
            for (int j = 0; j < lstSelectedUtilities.Items.Count; j++)
                if (lstUtilities.Items[i].Value == lstSelectedUtilities.Items[j].Value)
                    flag = 1;

            if (flag == 0)
            {
                lstSelectedUtilities.Items.Add(new ListItem(lstUtilities.Items[i].Text, lstUtilities.Items[i].Value));
            }
        }
        lstUtilities.Items.Clear();
    }
    protected void btnMove1_Click(object sender, EventArgs e)
    {
        if (lstUtilities.SelectedIndex == -1)
        {
            MessageBox.Show("Select Utility");
          //  Response.Write("<script>alert('Select Utility')</script>");
            return;
        }
        //foreach (ListItem l in lstSelectedUtilities.Items)
        //{
        //    if (lstUtilities.SelectedItem == l)
        //    {
        //        Response.Write("<script>alert('Already Added')</script>");
        //        return;
        //    }
        //}
        lstSelectedUtilities.Items.Add(new ListItem(lstUtilities.SelectedItem.ToString(), lstUtilities.SelectedValue));
        lstUtilities.Items.RemoveAt(lstUtilities.SelectedIndex);
        
    }

    protected void btnAddUtilityGroup_Click(object sender, EventArgs e)
    {
        Session["UserManager_Control"] = "UserManager/ucSearchUtilityGroup.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");      
    }
}
